//
//  XJ_PopUpView.m
//  XunJieProject
//
//  Created by 丹丹 on 2018/10/31.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_PopUpView.h"

@interface XJ_PopUpView()<UITableViewDelegate,UITableViewDataSource>
// 背景视图
@property (nonatomic, strong) UIControl *backgroundView;
//弹窗
@property (nonatomic,retain) UITableView *listTable;

@end

@implementation XJ_PopUpView

- (UITableView *)listTable{
    
    
    if (_listTable == nil) {
        
        _listTable = [[UITableView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - _cellWidth)/2, (SCREEN_HEIGHT - 44*_cellCount)/2, _cellWidth, 44*_cellCount) style:UITableViewStylePlain];
        _listTable.delegate = self;
        _listTable.dataSource = self;
        _listTable.layer.cornerRadius = 10;

    }
    return _listTable;
}
- (instancetype)initWithFrame:(CGRect)frame andTableCellWidth:(NSInteger)cellWidth andCellCount:(NSInteger)cellCount
{
    if (self == [super initWithFrame:frame]) {
        
        _cellWidth = cellWidth;
        _cellCount = cellCount;
        _backgroundView = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backgroundView.backgroundColor = [UIColor blackColor];
        _backgroundView.alpha = 0.5;
        [_backgroundView addTarget:self action:@selector(cancelControl) forControlEvents:UIControlEventTouchUpInside];
//        UIWindow * window = [UIApplication sharedApplication].keyWindow;
        [self addSubview:_backgroundView];
        
        [self addSubview:self.listTable];
        
        
    }
    return self;
}

-(void)cancelControl{
    
    [self removeFromSuperview];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _cellArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
    }
    
    UILabel * contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _cellWidth, 44)];
    contentLabel.textAlignment = NSTextAlignmentCenter;
    contentLabel.text = [_cellArray objectAtIndex:indexPath.row];
    contentLabel.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:contentLabel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([_delegate respondsToSelector:@selector(selecteCellAtIndex:andType:)]) {
        
        
        if (_haveTitle) {
           
            if (indexPath.row !=0 ) {
                
                [_delegate selecteCellAtIndex:indexPath.row andType:_type];
            }
        }else{
            
            [_delegate selecteCellAtIndex:indexPath.row andType:_type] ;
        }
        
        
        [self removeFromSuperview];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
